﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ExcelDataImporter
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void OpenButtonClick(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            if(dialog.ShowDialog(this)==DialogResult.OK)
            {
                Session.Current.ExcelFile = dialog.FileName;
                InitSheetTabs();
            }
        }

        private void InitSheetTabs()
        {
            if (!string.IsNullOrEmpty(Session.Current.ExcelFile))
            {
                dataTabControl.TabPages.Clear();

                List<string> sheetNames = OleDbHelper.ReadSheetNames(Session.Current.ExcelFile);

                foreach (string sheetName in sheetNames)
                {
                    if (sheetName.EndsWith("$"))
                    {
                        SheetTabPage tabPage = new SheetTabPage(sheetName);
                        dataTabControl.TabPages.Add(tabPage);
                    }
                }
            }
        }

        private void GenerateButtonClick(object sender, EventArgs e)
        {
            SheetTabPage tabPage = dataTabControl.SelectedTab as SheetTabPage;
            if(tabPage!=null)
            {
                Session.Current.SelectedSheet = tabPage.SheetName;
                ImportForm importForm=new ImportForm();
                importForm.ShowDialog(this);
            }
            Session.Current.SelectedSheet = null;
        }
    }
}
